package com.nixelsolutions.realstate;

import java.io.File;
import java.util.HashMap;

import javax.servlet.annotation.WebServlet;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.jain.addon.i18N.component.I18NUI;
import com.jain.addon.resource.DefaultI18NResourceProvider;
import com.jain.addon.resource.I18NProvider;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.navigator.Navigator;
import com.vaadin.navigator.View;
import com.vaadin.server.ExternalResource;
import com.vaadin.server.FileResource;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinService;
import com.vaadin.server.VaadinServlet;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Link;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.Reindeer;

@SuppressWarnings("serial")
@Theme("realstate")
public class RealstateUI extends I18NUI implements Button.ClickListener{

	private static Logger logger = LogManager.getLogger(RealstateUI.class);
	private static I18NProvider provider = DefaultI18NResourceProvider.instance();
	private static String basepath = VaadinService.getCurrent().getBaseDirectory().getAbsolutePath();
	
	private static CssLayout root = new CssLayout();
	private static CssLayout header = new CssLayout();
	private static CssLayout content = new CssLayout();
	
	public enum Buttons {
	    aboutUs,
	    environment,
	    search,
	    best
	}
	
	private static Button aboutUs = new Button("about.us.title");
	private static Button environment = new Button("environment.title");
	private static Button search = new Button("search.title");
	private static Button best = new Button("best.title");
        
	@WebServlet(value = {"/*", "/VAADIN/*"}, asyncSupported = true)
	@VaadinServletConfiguration(productionMode = false, ui = RealstateUI.class)

	public static class Servlet extends VaadinServlet {
	}

	@Override
	protected void initialize(VaadinRequest request) {
		logger.info("Starting new session");
		logger.debug("Setting locale to: "+getLocale());
		System.out.println("Setting locale to: "+getLocale());
		root.addStyleName(Reindeer.LAYOUT_WHITE);
		setContent(root);
        root.setSizeFull();
        buildMainPage();
	}
	
	protected void addApplicationTitle() {
		getPage().setTitle(provider.getText(getLocale(), "application.title.name"));
	}
	
	protected void addMenu() {
		// LOGO
		VerticalLayout menu = new VerticalLayout();
		
		Link logoImage = new Link (null, new ExternalResource(provider.getText(getLocale(), "application.base.url")));
		logoImage.setIcon(new FileResource(new File(basepath +"/WEB-INF/images/logo.jpg")));
		logoImage.addStyleName("root-centered");
		
		// MENU
		HorizontalLayout menuButtons = new HorizontalLayout();
		menuButtons.setSpacing(true);		
		menuButtons.addStyleName("menu root-centered");

		aboutUs.addStyleName(Reindeer.BUTTON_LINK);
		environment.addStyleName(Reindeer.BUTTON_LINK);
		search.addStyleName(Reindeer.BUTTON_LINK);
		best.addStyleName(Reindeer.BUTTON_LINK);
		
		best.addClickListener(this);
		
		best.addStyleName("menu-active");	

		menuButtons.addComponent(new Embedded("", new FileResource(new File(basepath +"/WEB-INF/images/eleccion.gif"))));
		menuButtons.addComponent(aboutUs);
		menuButtons.addComponent(new Embedded("", new FileResource(new File(basepath +"/WEB-INF/images/eleccion.gif"))));
		menuButtons.addComponent(environment);
		menuButtons.addComponent(new Embedded("", new FileResource(new File(basepath +"/WEB-INF/images/eleccion.gif"))));
		menuButtons.addComponent(search);
		menuButtons.addComponent(new Embedded("", new FileResource(new File(basepath +"/WEB-INF/images/eleccion.gif"))));
		menuButtons.addComponent(best);
		
		Label horizontalLine = new Label ("<hr>", ContentMode.HTML);
		horizontalLine.addStyleName("menu");
		
		menu.addComponent(logoImage);
		menu.addComponent(menuButtons);
		menu.addComponent(horizontalLine);
		menu.addStyleName("root-centered");
		//header.addComponent(menu);
		root.addComponent(menu);
	}
	
	protected void buildMainPage() {
		addApplicationTitle();	
		addMenu();
	}

	@Override
	public void buttonClick(ClickEvent event) {
		// Differentiate targets by event source
		Buttons buttonPressed = Buttons.valueOf(event.getButton().getCaption());
		switch (buttonPressed) {
		case aboutUs:
			aboutUs.addStyleName("menu-active");
			break;
		case environment:
			environment.addStyleName("menu-active");
			break;
		case search:
			search.addStyleName("menu-active");
			break;
		case best:
			best.addStyleName("menu-active");
			break;
		}
	}
}